/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.member;

import entity.MemberCustomer;
import entity.UserLogin;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.MemberCustomerServiceLocal;
import view.member.forms.ProfileStrutsActionForm;

/**
 *
 * @author SONY
 */
public class ProfileStrutsAction extends org.apache.struts.action.Action {
    
    @EJB MemberCustomerServiceLocal memberCustomerServiceLocal;

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // Use Struts actions to record business processing errors.
        ActionErrors errors = new ActionErrors();
        // Store this set in the request scope, in case we need to send the ErrorPage view.
        saveErrors(request, errors);       
        try {
            ProfileStrutsActionForm profile_form = (ProfileStrutsActionForm) form;
            
            HttpSession session = request.getSession();
            MemberCustomer member = (MemberCustomer) session.getAttribute("user_member");
            ServletContext context = servlet.getServletContext();
            memberCustomerServiceLocal = (MemberCustomerServiceLocal) context.getAttribute("memberCustomerServiceLocal");
            if(memberCustomerServiceLocal.updateMember_UserLogin(member.getMemberID(), profile_form.getFirstName(), profile_form.getLastName(), 
                    profile_form.getAddress(), profile_form.getPhone(), profile_form.getGender(), 
                    profile_form.getEmail(), profile_form.getPasswordNew(), profile_form.getChangePass())) { 
                
                //xóa info member trong session cũ
                session.removeAttribute("user_member");
                MemberCustomer new_member_info = new MemberCustomer(member.getMemberID(), profile_form.getFirstName(), profile_form.getLastName(), 
                        profile_form.getAddress(), profile_form.getPhone(), profile_form.getGender());
                //set info member mới vừa cập nhật vào session
                session.setAttribute("user_member", new_member_info);
                
                profile_form.setChangePass(false);
                profile_form.setPasswordOld("");
                profile_form.setPasswordNew("");
                profile_form.setConfirmpassNew("");
                request.setAttribute("success", "Cập Nhật thành công!");
                return mapping.findForward(SUCCESS);
            }
        } catch(Exception e) {
            System.err.println(e);
        }
        request.setAttribute("error", "Cập Nhật Thất Bại Do Bị Gián Đoạn. Hãy Cập Nhật Lại Sau!");
        return mapping.findForward("error");
    }
}
